package com.bwie.task;

import cn.hutool.core.bean.BeanUtil;
import com.bwie.mapper.DishesMapper;
import com.bwie.pojo.Dishes;
import com.bwie.pojo.EsDishes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lenovo
 */

@Component
public class EsTask {

    @Autowired
    private DishesMapper dishesMapper;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void esTask() {
        System.out.println("定时任务执行");
        List<Dishes> dishesList  = dishesMapper.selectAllList();

        for (Dishes dishes : dishesList) {
            EsDishes esDishes = BeanUtil.toBean(dishes, EsDishes.class);
            elasticsearchRestTemplate.save(esDishes);
        }
    }
}
